package zhh.tangbao.auxiliary.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zhh.tangbao.auxiliary.dto.message.SystemMessageQueryDto;
import zhh.tangbao.auxiliary.entity.SystemMessage;
import zhh.tangbao.auxiliary.service.SystemMessageService;
import zhh.tangbao.auxiliary.vo.message.MessageCountStatisticsVo;
import zhh.tangbao.system.annotation.Authority;
import zhh.tangbao.system.result.Result;
import zhh.tangbao.system.utils.UserHolderUtil;

import java.util.List;

/**
 * <p>
 * 描述
 * </p>
 *
 * @author 粥灰灰
 * <p>2023/4/25</p>
 */

@RestController
@RequestMapping("/api/message")
public class SystemMessageController {

    @Autowired
    private SystemMessageService service;

    @Autowired
    private UserHolderUtil userHolderUtil;

    @GetMapping("/messageCount")
    @Authority
    public Result getCurrentUserMessageCount() {
        MessageCountStatisticsVo vo = service.getUserMessageCount(userHolderUtil.getCurrentUser().getId());
        return Result.success().put(vo);
    }

    @GetMapping("/page")
    @Authority
    public Result page(SystemMessageQueryDto dto) {
        Page<SystemMessage> page = service.page(dto);
        return Result.success().put(page);
    }

    @DeleteMapping("/batchDelete")
    @Authority
    public Result batchMessage(@RequestBody List<String> ids) {
        return Result.successOrFail(service.batchDelete(ids));
    }

    @GetMapping("/readMessage/{id}")
    @Authority
    public Result readMessage(@PathVariable("id") String messageId) {
        return Result.successOrFail(service.readMessage(messageId));
    }

    @PutMapping("/markItAsRead")
    @Authority
    public Result markItAsRead(@RequestBody List<String> messageIds) {
        return Result.successOrFail(service.markItAsRead(messageIds));
    }

    @PutMapping("/markAllAsRead")
    @Authority
    public Result markAllAsRead() {
        return Result.successOrFail(service.markAllAsRead());
    }

    @GetMapping("/get/{id}")
    public Result get(@PathVariable("id") String id) {
        SystemMessage vo = service.getInfo(id);
        return Result.success().put(vo);
    }
}
